package book

import (
	"fmt"
	"net/http"

	"gin_web/apps/alipay"
	"github.com/gin-gonic/gin"
)

func BooksPage(c *gin.Context) {
	c.HTML(http.StatusOK, "index.html", gin.H{"name": "1"})
	// c.HTML(http.StatusOK, "index.html", gin.H{})
}

func GetBooks(c *gin.Context) {
	// 实现获取书籍列表的逻辑
	books := []string{"Book 1", "Book 2", "Book 3"}
	c.JSON(http.StatusOK, gin.H{"books": books})

}

func GetBookByID(c *gin.Context) {
	id := c.QueryArray("id")
	fmt.Println(id)
	c.JSON(http.StatusOK, gin.H{"books": id})
}

type Book struct {
	Title  string `json:"title"`
	Author string `json:"author"`
}

func CreateBook(c *gin.Context) {
	var newBook Book
	if err := c.BindJSON(&newBook); err != nil {
		return
	}
	// 使用newBook中的数据创建书籍
}

func UpdateBook(c *gin.Context) {
	// id := c.Param("id")
	// 实现更新书籍的逻辑
}

func DeleteBook(c *gin.Context) {
	// id := c.Param("id")
	// 实现删除书籍的逻辑
	// c.JSON(http.StatusOK, gin.H{"status": []string{
	//	"success",
	// }})
}

func CreateBooksTrade(c *gin.Context) {
	c.Param("id")

	// 从前端接收订单参数
	trade := alipay.AliPayTrade{
		NotifyURL:   "http://127.0.0.1:8080/alipay/notify",
		ReturnURL:   "http://127.0.0.1:8080/alipay/return",
		Subject:     "测试订单",
		ProductID:   1,
		OutTradeNo:  1,
		TotalAmount: 1,
	}
	url := trade.CreateTrade()

	c.Redirect(http.StatusFound, url)

}
